<?php
namespace app\admin\controller;
//常见问题
class Problem extends Common
{
	//问题列表
    public function problem_list()
    {
		$where="a.id > 0";
		$name=Request()->param('name');
		$status=Request()->param('status');
		$start_time=Request()->param('start_time');
		$end_time=Request()->param('end_time');
		$page=Request()->param('page');
		$size=Request()->param('size');
		if($page<1){
			$page=1;
		}
		if($size<15){
			$size=15;
		}
		$now=($page-1)*$size;
		if($name!=''){
			$where.=" and a.p_title like '%".$name."%'";
		}
		if($status>0){
			if($status==1){
				$where.=" and a.p_status = 1";
			}else{
				$where.=" and a.p_status != 1";
			}
			
		}
		if(strtotime($start_time)>946656000){
			$start_time=strtotime($start_time);
			$where.=" and a.p_create_time >= $start_time";
		}else{
			$start_time=0;
		}
		if(strtotime($end_time)>946656000){
			$end_time=strtotime($end_time);
			$where.=" and a.p_create_time <= $end_time";
		}else{
			$end_time=0;
		}
		$data=db('problem a')
			->where($where)
			->limit("$now,$size")
			->order("a.p_sort desc")
			->select();
		$count=db('problem a')
			->where($where)
			->count();
		//等级信息
		$page_str=page($page,$size,$count,count($data));//分页
		$this->assign('data',$data);
		$this->assign('count',$count);
		$this->assign('name',$name);
		$this->assign('status',$status);
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->assign('page',$page);
		$this->assign('size',$size);
		$this->assign('page_str',$page_str);
		return $this->fetch();
    }
	//添加问题
	function add_problem(){
		if(Request()->isPost()){
			$data=Request()->post();
            $data['p_title']=trim($data['p_title']);
            //问题标题
            if($data['p_title']==''){
                return return_json(array('code'=>0,'msg'=>'标题不能为空'),'添加常见问题');
            }
            if($data['p_sort']==''){
                $data['p_sort']=0;
            }
            if($data['editorValue']==''){
                return return_json(array('code'=>0,'msg'=>'请输入详情'),'添加常见问题');
            }
            $data['p_content']=$data['editorValue'];
            unset($data['editorValue']);
			$problem=db('problem')->where(array('p_title'=>$data['p_title']))->find();
            if(!empty($problem)){
                return return_json(array('code'=>0,'msg'=>'已有该标题的常见问题'),'添加常见问题');
            }
            $data['p_create_time']=time();
            $data['p_add_user_id']=session('uid');
            $data['p_add_user_name']=session('user.real_name');
            $res=db('problem')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'常见问题添加成功'),'添加常见问题');
            }else{
                return return_json(array('code'=>0,'msg'=>'常见问题添加失败'),'添加常见问题');
            }
		}else{
			return $this->fetch();
		}
	}
	//问题冻结
	function problem_del(){
		if(Request()->isAjax()){
			$id=Request()->post('id');
			$res=db('problem')
				->where(array('id'=>$id))
				->update(array('p_status'=>2,'p_update_time'=>time()));
			if($res){
				return return_json(array('code'=>1,'msg'=>'冻结成功！'),'冻结banner');die;
			}else{
				return return_json(array('code'=>0,'msg'=>'冻结失败！'),'冻结banner');die;
			}
		}
	}
	//问题解冻
	function problem_use(){
		if(Request()->isAjax()){
			$id=Request()->post('id');
			$res=db('problem')
				->where(array('id'=>$id))
				->update(array('p_status'=>1,'p_update_time'=>time()));
			if($res){
				return return_json(array('code'=>1,'msg'=>'解冻成功！'),'解冻banner');die;
			}else{
				return return_json(array('code'=>0,'msg'=>'解冻失败！'),'解冻banner');die;
			}
		}
	}
	//添加banner
	function reproblem(){
		if(Request()->isPost()){
            $data=Request()->post();
            $id=$data['id'];
            unset($data['id']);
            $data['p_title']=trim($data['p_title']);
            //问题标题
            if($data['p_title']==''){
                return return_json(array('code'=>0,'msg'=>'标题不能为空'),'修改常见问题');
            }
            if($data['p_sort']==''){
                $data['p_sort']=0;
            }
            if($data['editorValue']==''){
                return return_json(array('code'=>0,'msg'=>'请输入详情'),'修改常见问题');
            }
            $data['p_content']=$data['editorValue'];
            unset($data['editorValue']);
            $problem=db('problem')->where(array('p_title'=>$data['p_title'],'id'=>array('neq',$id)))->find();
            if(!empty($problem)){
                return return_json(array('code'=>0,'msg'=>'已有该标题的常见问题'),'修改常见问题');
            }
            $data['p_update_time']=time();
            $res=db('problem')->where(array('id'=>$id))->update($data);
            //添加权限
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'常见问题修改成功'),'修改常见问题');
            }else{
                return return_json(array('code'=>0,'msg'=>'常见问题修改失败'),'修改常见问题');
            }
		}else{
			$id=Request()->get('id');
			$problem=db('problem')
				->where(array('id'=>$id))
				->find();
			$this->assign('id',$id);
			$this->assign('problem',$problem);
			return $this->fetch();
		}
	}
}